group_by(country_year) %>%
summarise(sumNA = sum(is.na(gini_mkt)))
missing
data <- data %>%
mutate(gini_mkt=ifelse(country_year=="Algeria 2014", 33.2, gini_mkt)) %>%
mutate(gini_disp=ifelse(country_year=="Algeria 2014", 32.2, gini_disp)) %>%
mutate(gini_mkt=ifelse(country_year=="Azerbaijan 2011", 31.5, gini_mkt)) %>%
mutate(gini_disp=ifelse(country_year=="Azerbaijan 2011", 23.3, gini_mkt))
data <- data %>%
mutate(gini_mkt=ifelse(country_year=="Haiti 2016", 58.3, gini_mkt)) %>%
mutate(gini_disp=ifelse(country_year=="Haiti 2016", 53.3, gini_disp)) %>%
mutate(gini_mkt=ifelse(country_year=="Nigeria 2012", 43, gini_mkt)) %>%
mutate(gini_disp=ifelse(country_year=="Nigeria 2012", 42.2, gini_mkt)) %>%
mutate(gini_mkt=ifelse(country_year=="Zimbabwe 2012", 45.7, gini_mkt)) %>%
mutate(gini_disp=ifelse(country_year=="Zimbabwe 2012", 45.3, gini_mkt))
missing <- data %>%
group_by(country_year) %>%
summarise(sumNA = sum(is.na(gini_mkt)))
missing
View(missing)
rm(missing)
data <- data %>%
drop_na(gini_mkt)
missing <- data %>%
group_by(country_year) %>%
summarise(sumNA = sum(is.na(gini_mkt)))
missing
View(missing)
rm(missing)
table(data$country_year, is.na(data$v2elvotbuy))
# Author: Zoltan Fazekas
centFUN <- function(x) {
x - mean(x, na.rm = TRUE)
}
# Median Cenetering for factor variables
centFUN_median <- function(x) {
x - median(x, na.rm = TRUE)
}
# Use the function to grand-mean center the macro-level predictor
data$gini_mktCGM <- centFUN(data$gini_mkt)
data$gini_dispCGM <- centFUN(data$gini_disp)
data$v2xel_frefairCGM <- centFUN(data$v2xel_frefair)
data$e_migdppclnCGM <- centFUN(data$e_migdppcln)
data$v2elvotbuyCGM <- centFUN(data$v2elvotbuy)
summary(data$e_migdppclnCGM)
summary(data$v2xel_frefairCGM)
summary(data$gini_mktCGM)
summary(data$gini_dispCGM)
summary(data$v2elvotbuyCGM)
data <- data %>%
group_by(S025) %>%
mutate(ageCWC = centFUN(age))
summary(data$ageCWC)
data <- data %>%
mutate(income_quintiles = ifelse(income_deciles==1 | 2, 1,
ifelse(income_deciles==3 | 4, 2,
ifelse(income_deciles==5 | 6, 3,
ifelse(income_deciles==7 | 8, 4, 5)))))
summary(data$income_quintiles)
data <- data %>%
mutate(income_quintiles = ifelse(income_deciles==1, 1,
ifelse(income_deciles==2, 1,
ifelse(income_deciles==3, 2,
ifelse(income_deciles==4, 2,
ifelse(income_deciles==5, 3,
ifelse(income_deciles==6, 3,
ifelse(income_deciles==7 , 4,
ifelse(income_deciles==8, 4, 5)))))))))
summary(data$income_quintiles)
data <- data %>%
group_by(S025) %>%
mutate(educationCWC = centFUN(as.numeric(education)),
income_quintilesCWC = centFUN(as.numeric(income_quintiles)),
income_decilesCWC = centFUN(as.numeric(income_deciles)))
summary(wvsdata$educationCWC)
summary(wvsdata$income_quintilesCWC)
summary(wvsdata$income_decilesCWC)
summary(data$educationCWC)
summary(data$income_quintilesCWC)
summary(data$income_decilesCWC)
summary(data$education)
summary(data$sex)
summary(data$ageCWC)
summary(data$income_quintiles)
summary(data$S003)
data$ageCWC <- data$ageCWC/10
library(lme4)
v1 <- glmer(vote ~ 1 + (1 | S025) + (1 | S003) ,
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v1)
library(sjstats)
icc(v1, adjusted = TRUE)
icc(v1)
v2 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployment + income_quintilesCWC + (1 | S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v2)
anova(v1, v2)
v2 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemploymed + income_quintilesCWC + (1 | S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v2)
v2 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployed + income_quintilesCWC + (1 | S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v2)
anova(v1, v2)
v3 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployed + income_quintilesCWC +
gini_mktCGM +
(1 | S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v3)
anova(v2, v3)
v4 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployed + income_quintilesCWC +
gini_mktCGM + v2xel_frefairCGM + e_migdppclnCGM + v2elvotbuyCGM +
(1 | S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v4)
anova(v2, v4)
# model 4 plus random slopes
v5 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployed + income_quintilesCWC +
gini_mktCGM + v2xel_frefairCGM + e_migdppclnCGM + v2elvotbuyCGM +
(1 + income_quintilesCWC| S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v5)
anova(v2, v5)
# model 5 plus random slopes
v6 <- glmer(vote ~ sex + ageCWC+ educationCWC + children +
unemployed + income_quintilesCWC +
gini_mktCGM + v2xel_frefairCGM + e_migdppclnCGM + v2elvotbuyCGM +
income_quintilesCWC*gini_mktCGM + v2elvotbuyCGM*income_quintilesCWC +
(1 + income_quintilesCWC| S025) + (1 | S003),
data = data,
na.action = na.omit,
family = binomial(link = "logit"),
control=glmerControl(optCtrl=list(maxfun=6000)))
summary(v6)
anova(v5, v6)
library(interplot)
plot_Interaction_vote_mkt <- interplot(v6,
var1 = "income_quintilesCWC",
var2 = "gini_mktCGM",
ci = 0.95,
hist = TRUE) +
xlab("Inequality (market)") +
ylab("Effect of income on individual voting") +
theme_bw() +
geom_hline(yintercept = 0,
linetype = "dashed",
color = "red",
size = 0.5)
plot_Interaction_vote_mkt
plot_Interaction_vote_mkt2 <- interplot(v6,
var1 = "gini_mktCGM",
var2 = "income_quintilesCWC",
ci = 0.95,
hist = TRUE) +
xlab("Individual Income") +
ylab("Effect of inequality on individual voting") +
theme_bw() +
geom_hline(yintercept = 0,
linetype = "dashed",
color = "red",
size = 0.5)
plot_Interaction_vote_mkt2
plot_Interaction_vote_buying <- interplot(v6,
var1 = "income_quintilesCWC",
var2 = "v2elvotbuyCGM",
ci = 0.95,
hist = TRUE) +
xlab("Vote Buying") +
ylab("Effect of income on individual voting") +
theme_bw() +
geom_hline(yintercept = 0,
linetype = "dashed",
color = "red",
size = 0.5)
plot_Interaction_vote_buying
plot_Interaction_vote_buying2 <- interplot(v6,
var1 = "v2elvotbuyCGM",
var2 = "income_quintilesCWC",
ci = 0.95,
hist = TRUE) +
xlab("Individual Income") +
ylab("Effect of vote buying on individual voting") +
theme_bw() +
geom_hline(yintercept = 0,
linetype = "dashed",
color = "red",
size = 0.5)
plot_Interaction_vote_buying2
library(dotwhisker)
library(broom.mixed)
v1_df <- tidy(v1, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v2_df <- tidy(v2, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v3_df <- tidy(v3, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v4_df <- tidy(v4, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v5_df <- tidy(v5, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v6_df <- tidy(v6, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v1_df <- v1_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 1") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployment = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v1_df <- tidy(v1, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v2_df <- tidy(v2, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v3_df <- tidy(v3, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v4_df <- tidy(v4, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v5_df <- tidy(v5, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v6_df <- tidy(v6, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
#Rescaling ordinal and continous predictors by tow standard deviations
v1_df <- v1_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 1") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v2_df <- v2_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 2") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v3_df <- v3_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 3") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v4_df <- v4_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 4") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v5_df <- v5_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 5") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v6_df <- v6_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 6") %>%
relabel_predictors(c(sexMale = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotebuyCGM = "Vote Buying (centered)"))
v_models <- rbind(v2_df, v3_df, v4_df, v5_df, v6_df)
v_brackets <- list(c("Individual Predictors", "Male", "Income (centered)"),
c("Country-year Predcitors", "Market Inequality (centered)", "Vote Buying (centered)"),
c("Cross level Interactions", "income_quintilesCWC:gini_mktCGM" , "income_quintilesCWC:2velvotbuyCGM"))
plot_main_vote <- {dwplot(v_models) +
theme_bw() + xlab("Coefficient Estimate") + ylab("") +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
ggtitle("Predicting Voting") +
theme(plot.title = element_text(face="bold"),
legend.position = c(0.85, 0.8),
legend.justification = c(0, 0),
legend.background = element_rect(colour="grey80"),
legend.title = element_blank())} %>%
add_brackets(v_brackets)
plot_main_vote
v1_df <- tidy(v1, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v2_df <- tidy(v2, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v3_df <- tidy(v3, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v4_df <- tidy(v4, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v5_df <- tidy(v5, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
v6_df <- tidy(v6, conf.int=TRUE, exponentiate=FALSE, effects="fixed",
conf.method = "Wald")
#Rescaling ordinal and continous predictors by tow standard deviations
v1_df <- v1_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 1") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v2_df <- v2_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 2") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v3_df <- v3_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 3") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v4_df <- v4_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 4") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v5_df <- v5_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 5") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v6_df <- v6_df %>%
by_2sd(data) %>%
arrange(term) %>%
mutate(model="Model 6") %>%
relabel_predictors(c(sex = "Male",
ageCWC = "Age (centered)",
educationCWC = "Education (centered)",
children = "Children",
unemployed = "unemployed",
income_quintilesCWC = "Income (centered)",
gini_mktCGM = "Market Inequality (centered)",
v2xel_frefairCGM = "Fariness election (centered)",
e_migdppclnCGM = "GDP pc log (centered)",
v2elvotbuyCGM = "Vote Buying (centered)"))
v_models <- rbind(v2_df, v3_df, v4_df, v5_df, v6_df)
v_brackets <- list(c("Individual Predictors", "Male", "Income (centered)"),
c("Country-year Predcitors", "Market Inequality (centered)", "Vote Buying (centered)"),
c("Cross level Interactions", "income_quintilesCWC:gini_mktCGM" , "income_quintilesCWC:2velvotbuyCGM"))
plot_main_vote <- {dwplot(v_models) +
theme_bw() + xlab("Coefficient Estimate") + ylab("") +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
ggtitle("Predicting Voting") +
theme(plot.title = element_text(face="bold"),
legend.position = c(0.85, 0.8),
legend.justification = c(0, 0),
legend.background = element_rect(colour="grey80"),
legend.title = element_blank())} %>%
add_brackets(v_brackets)
plot_main_vote
v_brackets <- list(c("Individual Predictors", "Male", "Income (centered)"),
c("Country-year Predcitors", "Market Inequality (centered)", "Vote Buying (centered)"),
c("Cross level Interactions", "income_quintilesCWC:gini_mktCGM" , "income_quintilesCWC:v2elvotbuyCGM"))
plot_main_vote <- {dwplot(v_models) +
theme_bw() + xlab("Coefficient Estimate") + ylab("") +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
ggtitle("Predicting Voting") +
theme(plot.title = element_text(face="bold"),
legend.position = c(0.85, 0.8),
legend.justification = c(0, 0),
legend.background = element_rect(colour="grey80"),
legend.title = element_blank())} %>%
add_brackets(v_brackets)
plot_main_vote
###Loading Packages###
library(countrycode)
library(tidyverse)
library(viridis)
getwd()
setwd("M:/Promotion/Paper/WVS Redistribution in Autocracies")
#Import Data
setwd("M:/Promotion/Data/Country_Year_V-Dem_Full+others_STATA_v9")
#Load DATA
vdem <- read_csv("V-Dem-CY-Full+Others-v9.csv")
vdem$cowcode <- vdem$COWcode
setwd("M:/Promotion/Data/WVS")
wvsdata <- readRDS("F00008390-WVS_Longitudinal_1981_2016_r_v20180912.rds")
head(wvsdata)
#Loading Solt SWID DATA##
setwd("M:/Promotion/Data/Solt 2019 SWIID 8.0")
swiid <- read_csv("SWIID.csv")
#Adding COW Country Codes to the WVS Data to merge with VDEM DATA
wvsdata <- wvsdata%>%
select(starts_with("S"), E035, E037, starts_with("X"))
wvsdata$cowcode <- countrycode(wvsdata$S003, "wvs", "cown", warn = TRUE)
wvsdata$year <- wvsdata$S020
vdem <- vdem %>%
select(country_name,country_id, year, country_text_id, COWcode, cowcode, v2x_polyarchy,
v2x_libdem, v2x_partipdem, v2x_delibdem, v2x_egaldem, v2x_regime, starts_with("v2xreg"),
starts_with("v2pe"), starts_with("v2cl"), starts_with("v2exl"), v2x_neopat,
starts_with("v2xnp"), e_migdppcln, e_migdpgro, e_mipopula, e_miurbpop)
vdem <- vdem %>%
filter(year>1980)
setwd("M:/Promotion/Data/WVS")
wvsdata <- readRDS("F00008390-WVS_Longitudinal_1981_2016_r_v20180912.rds")
